﻿using GongXianDCUI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GongXianDCUI.BLL;
using Webdiyer.WebControls.Mvc;

namespace GongXianDCUI.Controllers
{
    public class FenBuListController : Controller
    {
        //文章列表
        public ActionResult ArticleList(int? id, int? pageIndex = 1)
        {

            ArticleBLL articBll = new ArticleBLL();
            if (id == null)
            {
                return RedirectToAction("Index", "Home");
            }
            //验证跳转文章详细
            goPageArticle(id);
            HomeController hc = new HomeController();
            List<Funcitions> list = hc.GetFunctionList(HttpContext);//左侧菜单模块列表
            List<Funcitions> list2 = list.Where(w => w.FunctionId == id).ToList();
            Funcitions model = null;
            if (list2 == null || list2.Count == 0)
            {
                //点击的是子菜单
                foreach (var item in list)
                {
                    if (item.Childrens.Count > 0)
                    {
                        list2 = item.Childrens.Where(w => w.FunctionId == id).ToList();
                        if (list2 != null && list2.Count > 0)
                        {
                            //表示找到子菜单了
                            model = list2.First();
                            break;
                        }
                    }
                }
            }
            else
            {
                model = list2.First();
                model.ChildrenName = "";//点击的根节点
            }
            Funcitions modelDynmic = model;
            //点击3级菜单时，重新找所属2级
            ViewBag.miniName = "";
            if (model == null)
            {
                FunctionsBLL bll = new FunctionsBLL();
                Funcitions db = bll.getFun(id);
                list2 = list.Where(w => w.FunctionId == db.ParentId).ToList();
                if (list2 == null || list2.Count == 0)
                {
                    //点击的是子菜单
                    foreach (var item in list)
                    {
                        if (item.Childrens.Count > 0)
                        {
                            list2 = item.Childrens.Where(w => w.FunctionId == db.ParentId).ToList();
                            if (list2 != null && list2.Count > 0)
                            {
                                //表示找到子菜单了
                                model = list2.First();
                                ViewBag.miniName = db.FunctionName;
                                break;
                            }
                        }
                    }

                }
                else
                {
                    model = list2.First();
                    model.ChildrenName = "";//点击的根节点
                }

            }
            if (model != null)
            {
                if (model.ParentId > 0)
                {
                    //表示点击的是子菜单
                    modelDynmic = list.Where(w => w.FunctionId == model.ParentId).First();//查询父级名称
                    modelDynmic.ChildrenName = model.FunctionName;//子模块名称’
                    modelDynmic.ChildrenID = model.FunctionId;
                }
            }
            //查询文章列表
            PagedList<Article> listPaing = null;
            if (id.Value < 100)
            {
                listPaing = articBll.GetArticleModelPage(pageIndex.Value, 20, id.Value, 1);
            }
            else
            {
                listPaing = articBll.GetArticlePage(pageIndex.Value, 20, id.Value, 1);
            }
            ViewBag.Menu = modelDynmic;

            List<Article> TopImages = articBll.GetArticleList(1401);
            ViewBag.topImage = TopImages;

            return View(listPaing);
        }


       /// <summary>
       /// 验证只需要一篇文章的菜单
       /// </summary>
       /// <param name="funcitonID"></param>
        public void goPageArticle(int? funcitonID)
        {
            if (funcitonID != null)
            {
                //公司简介，经营班子，组织机构。。。
                int[] idList = { 200, 201, 202 };
                if (idList.Contains(funcitonID.Value))
                {
                    ArticleBLL bll = new ArticleBLL();
                    long arid = bll.GetArticleList(funcitonID.Value).First().articleId;
                    Response.Redirect("~/FenBuList/Detail/" + arid);
                }
            }
        }
   


        //文章详细
        public ActionResult Detail(long? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index", "Home");
            }
            //根据文章id查询文章类别id
            int functionId = 0;//模块id
            ArticleBLL artibll = new ArticleBLL();
            Article aticleModel = artibll.GetModel(id.Value);//查询文章
            if (aticleModel != null)
            {
                functionId = aticleModel.categoryId.Value;
            }
            ViewBag.Article = aticleModel;//文章视图数据
            HomeController hc = new HomeController();
            List<Funcitions> list = hc.GetFunctionList(HttpContext);//模块列表
            List<Funcitions> list2 = list.Where(w => w.FunctionId == functionId).ToList();
            Funcitions model = null;
            if (list2 == null || list2.Count == 0)
            {
                //点击的是子菜单
                foreach (var item in list)
                {
                    if (item.Childrens.Count > 0)
                    {
                        list2 = item.Childrens.Where(w => w.FunctionId == functionId).ToList();
                        if (list2 != null && list2.Count > 0)
                        {
                            //表示找到子菜单了
                            model = list2.First();
                            break;
                        }
                    }
                }
            }
            else
            {
                model = list2.First();
                model.ChildrenName = "";//点击的根节点
            }
            Funcitions modelDynmic = model;


            if (model == null)
            {
                FunctionsBLL bll = new FunctionsBLL();
                Funcitions db = bll.getFun(functionId);
                list2 = list.Where(w => w.FunctionId == db.ParentId).ToList();
                if (list2 == null || list2.Count == 0)
                {
                    //点击的是子菜单
                    foreach (var item in list)
                    {
                        if (item.Childrens.Count > 0)
                        {
                            list2 = item.Childrens.Where(w => w.FunctionId == db.ParentId).ToList();
                            if (list2 != null && list2.Count > 0)
                            {
                                //表示找到子菜单了
                                model = list2.First();
                                ViewBag.miniName = db.FunctionName;
                                break;
                            }
                        }
                    }

                }
                else
                {
                    model = list2.First();
                    model.ChildrenName = "";//点击的根节点
                }

            }

            if (model.ParentId > 0)
            {
                //表示点击的是子菜单
                modelDynmic = list.Where(w => w.FunctionId == model.ParentId).First();//查询父级
                modelDynmic.ChildrenName = model.FunctionName;//子模块名称
                modelDynmic.ChildrenID = model.FunctionId;
            }
            List<Article> TopImages = artibll.GetArticleList(1401);
            ViewBag.topImage = TopImages;
            return View(modelDynmic);
        }



        public ActionResult JubaoBox()
        {
            ArticleBLL articlebll = new ArticleBLL();
            JuBaoBoxBLL jubaoBLL = new JuBaoBoxBLL();
            JuBaoBox juBaoBox = new JuBaoBox();
            int rtnValue = 0;
            juBaoBox.sGuid = Guid.NewGuid().ToString().Replace("-", "");
            juBaoBox.sTitle = Request["v_Title"] + "";
            juBaoBox.sJbr = Request["v_Jbr"] + "";
            juBaoBox.sUserNo = Request["v_UserNo"] + "";
            juBaoBox.sBeiJuBaoName = Request["v_BeiJuBaoName"] + "";
            juBaoBox.sContent = Request["v_Content"] + "";
            juBaoBox.sContent = System.Text.RegularExpressions.Regex.Replace(juBaoBox.sContent, "<[^>]+>", "");
            juBaoBox.sContent = System.Text.RegularExpressions.Regex.Replace(juBaoBox.sContent, "&[^;]+;", "") + "";
            juBaoBox.sJbUesrWork = Request["v_JbUesrWork"] + "";
            juBaoBox.sBeiJubaoUserWork = Request["v_BeiJubaoUserWork"] + "";
            juBaoBox.dCreateTime = DateTime.Now;
            if (juBaoBox.sTitle != "" && juBaoBox.sJbr != "" && juBaoBox.sUserNo != "" && juBaoBox.sBeiJuBaoName != "")
            {
                rtnValue = jubaoBLL.insertJuBaoBox(juBaoBox);
            }
            ViewBag.rtnValue = rtnValue;

            List<Article> TopImages = articlebll.GetArticleList(1401);
            ViewBag.topImage = TopImages;
            return View();
        }
    }
}